package cn.uniqueinfo.dp.controller;

import cn.uniqueinfo.dp.controller.base.BaseController;
import cn.uniqueinfo.dp.exception.UQException;
import cn.uniqueinfo.dp.service.IFormFieldService;
import cn.uniqueinfo.dp.util.ResultUtil;
import cn.uniqueinfo.dp.vo.FormFieldVO;
import cn.uniqueinfo.dp.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.beans.Transient;
import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-30 13:37:26
 * @description:表单字段配置，主要用在表单的新增和修改
 */
@Slf4j
@Controller
@RequestMapping(value = "/formfield")
public class FormFieldController extends BaseController<FormFieldVO> {
    private IFormFieldService service;

    public FormFieldController(IFormFieldService service) {
        this.service = service;
        this.baseService = service;
        this.ctlName = "formfield";
        this.tipName = "业务表单字段配置";
    }

    @GetMapping("/find")
    @ResponseBody
    public List<FormFieldVO> find(@RequestParam(value = "fn") String formName) {
        return this.service.findByFormName(formName);
    }

    @GetMapping("/sync")
    @ResponseBody
    @Transient
    public Result syncField(
            @RequestParam(value = "fn") String formName,
            @RequestParam(value = "tn") String tableName) {
        try {
            this.service.deleteByFormName(formName);
            Integer num = this.service.syncField(formName, tableName);
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            throw new UQException(0, "syncField", "同步字段失败", e);
        }
    }
}
